<?php

namespace App\Admin\Controllers;

use App\Models\Banner;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class BannersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.banners'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.banners'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.banners'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.banners'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Banner);

        $grid->id('Id');
        $grid->title(trans('admin.title'));
        $grid->image(trans('admin.image'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->description(trans('admin.description'));
        $grid->is_show(trans('admin.show'))->switch();
        $grid->actions(function ($actions) {
//            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->tools(function ($tools) {
            // 禁用批量删除按钮
            $tools->batch(function ($batch) {
//                $batch->disableDelete();
            });
        });
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('title', '标题');
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(Banner::findOrFail($id));
        $show->id('Id');
        $show->title(trans('admin.title'));
        $show->description(trans('admin.description'));
        $show->content(trans('admin.content'))->unescape()->as(function ($content) {
            return $content;
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Banner);

        $form->text('title', trans('admin.title'))->rules('required')->setLabelClass(['required']);
        $form->image('image', trans('admin.image'))->rules('required')->setLabelClass(['required']);
        $form->text('url', trans('admin.url'));
        $form->textarea('description', trans('admin.description'));
        $form->editor('content', trans('admin.content'));
        $form->switch('is_show', trans('admin.show'))->default(1);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        return $form;
    }
}
